<?php

/**
 * This is the model class for table "{{page}}".
 *
 * The followings are the available columns in table '{{page}}':
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property string $content
 * @property string $meta_description
 * @property string $meta_keywords
 * @property integer $layout_id
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Layout $layout
 */
class Page extends MyAR
{

    public $controlName = 'page';

    const STATUS_PUBLIC = 1;
    const STATUS_DRAFT  = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Page the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{page}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, url, content, layout_id, status', 'required'),
            array('layout_id, status', 'numerical', 'integerOnly' => true),
            array('title, url, meta_description, meta_keywords', 'length', 'max' => 200),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, url, content, meta_description, meta_keywords, layout_id, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'layout' => array(self::BELONGS_TO, 'Layout', 'layout_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'               => 'ID',
            'title'            => 'Title',
            'url'              => 'Url',
            'content'          => 'Content',
            'meta_description' => 'Meta Description',
            'meta_keywords'    => 'Meta Keywords',
            'layout_id'        => 'Layout',
            'status'           => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('meta_description', $this->meta_description, true);
        $criteria->compare('meta_keywords', $this->meta_keywords, true);
        $criteria->compare('layout_id', $this->layout_id);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    static function getStatusArray()
    {
        return array(
            self::STATUS_PUBLIC => 'Public',
            self::STATUS_DRAFT  => 'Draft',
        );
    }

}
